<!--setOrgInfo():返回数据id-->
<!--setOrgname():返回审核人-->
<!--orgTree:true   选择机构-->
<!--codeAffordIf:true   筛选管理机构-->
<!--currentOrgId:orgId   用于修改页面默认展示当前人员的机构ID-->
<!--currentOrgName:orgName   用于修改页面默认展示当前人员的机构Name-->
<!--setCurrentName:{value:123,label:'王德发'}   组件默认显示-->
<!--multiple:true   可多选下级审核人-->
<!--department:true  可选择部门--> 
<!--rootOrgId 从该机构id -->

<template>
    <div class="Cascader">
        <Poptip v-model="visible" style="width:100%"  placement="bottom-start" transfer @on-popper-show="onPopperShow">
            <Input clearable v-model="inputOrgName" style="width:100%"  placeholder="请输入送检单位"></Input>
            <div slot="content" style="height:200px;overflow-y:scroll;width:100%">
            <Tree ref="tree" :data="orgTreeData" :load-data="handleLoadData" :render="renderContent" style="width: 100%" :multiple="multiple"></Tree>
            </div>
        </Poptip>
    </div>
</template>

<script>
    import { sessionStorage } from '../common/cacheUtils.js';
    import {Api} from '../api/index.js'

    export default {
        name:"treeDom",
        props: {
            orgTree:{
                type: Boolean,
                default: false
            },
            codeAffordIf:{
                type: String,
                default: '0'
            },
            multiple:{
                type: Boolean,
                default: false
            },
            department:{
                type: Boolean,
                default: false
            },
            setselectionarr:{
                type: Array,
                default: ()=>[]
            },
            setCurrentName:{
            },
            currentOrgId : {
            },
            currentOrgName : {
            },
            rootOrgId: null
        },
        data() {
            return {
                orgTreeData:[],
                inputOrgName: "",
                visible: false
            }
        },
        methods: {
            renderContent (h, { root, node, data }) {
                let icon = "ios-folder";
                return h('span', {
                    style: {
                        display: 'inline-block',
                        cursor: "pointer",
                        color: node.node.selected ? "#2d8cf0" : "black" //根据选中状态设置样式
                    },
                    on:{
                        click: () => {
                            console.log(data)
                            this.visible = false;
                            if(data.type =="org"){
                                this.$emit('setOrgInfo',data.orgId)
                                this.$emit('setOrgname',data.orgName)
                                //this.currentOrgId = data.orgId;
                                this.inputOrgName = data.orgName;
                            }else{
                                this.$emit('setOrgInfo',data.userId)
                                this.$emit('setOrgname',data.userName)
                                //this.currentOrgId = data.userId;
                                this.inputOrgName = data.userName;
                            }
                        }
                    }
                },
                [
                    h('span', [
                        h('Icon', {
                            props: {
                                type: icon,
                            },
                            style: {
                                marginRight: '5px',
                                color: '#FF7F00'
                            }
                        }),
                        h('span', data.orgName)
                    ])
                ]
                );
            },
            handleLoadData (item, callback) {
                setTimeout(() => {
                if(this.orgTree){
                    this.$http.get(Api.orgInfoTree,{
                        params:{
                            parentId:item.orgId,
                            codeAffordIf:this.codeAffordIf
                        }
                    }).then(res => {
                        item.loading = false
                        var resData = [];
                        if(!this.department)
                        {
                            res.data.data.map(i=>{
                                if(i.orgType == "1"){
                                    i.type = "org"
                                    i.label=i.orgName;
                                    i.loading=false;
                                    i.children = [];
                                    resData.push(i);
                                }
                            })
                        }else{
                            res.data.data.map(i=>{
                                 i.type = "org"
                                i.label=i.orgName;
                                i.loading=false
                                i.children = [];
                                resData.push(i);
                            }) 
                        } 
                        callback(resData);
                    }).catch(e => {
                    })
                }else{
                    this.$http.get(Api.orgInfoTree,{
                        params:{
                            parentId:item.orgId,
                            codeAffordIf:this.codeAffordIf
                        }
                    }).then(res => {
                        if(res.data.data.length!=0){
                            res.data.data.map(i=>{
                                i.label=i.orgName;
                                i.type = "org"
                                i.loading=false
                                i.children = [];
                            })
                            item.children = res.data.data
                            item.loading=false
                            callback();
                        }else{
                            delete item['children']
                            item.loading=false
                            delete item['loading']

                            this.$http.get(Api.orgUsers+item.value,{params:{}}).then((res) => {
                                if(res.data.data.length==0){
                                    res.data=[{value:'',label:'暂无人员',disabled:true}]
                                }else{
                                    res.data.data.map(j=>{
                                        j.label = j.userName
                                        j.type = "user"
                                    })
                                }
                                callback(res.data.data);
                            }).catch(e => {
                                console.log(e)
                            })
                        }
                    }).catch(e => {
                        console.log(e)
                    })
                }
                }, 2000);
            },
            onPopperShow(){
                let reqOrgId = '';
                if(this.rootOrgId!=undefined){
                    reqOrgId = this.rootOrgId;
                }
                else if(sessionStorage('userId').get('userId')){
                    reqOrgId = JSON.parse(sessionStorage('userId').getObject('userId').companyId);
                }else{
                    reqOrgId = -1;
                }
                this.$http.get(Api.parentOrgId+reqOrgId,{params:{}}).then(o => {
                    let parentId = o.data.data.orgId;
                    if(reqOrgId != -1){
                        this.orgTreeData = [{
                            orgId:o.data.data.orgId,
                            type:"org",
                            label:o.data.data.orgName,
                            orgName:o.data.data.orgName,
                            children:[]
                        }]
                    }
                    this.$http.get(Api.orgInfoTree,{
                        params:{
                            //parentId: '-1',
                            parentId: parentId,
                            codeAffordIf:this.codeAffordIf
                        }
                    }).then(res => {
                        res.data.data.forEach(i=>{
                            i.label = i.orgName
                            i.type = "org"
                            i.loading=false
                            i.children = [];
                        })
                        if(reqOrgId != -1){
                            this.orgTreeData[0].children = res.data.data;
                        }else{
                            this.orgTreeData = res.data.data;
                        }
                    }).catch(e => {
                    })
                }).catch(e => {
                })
            },
            setCurrentOrg(){
                this.inputOrgName = this.currentOrgName;
            },
            setCurrentUser(){
                this.inputOrgName = this.currentOrgName;
            },
        },
        computed: {
        },
        mounted() {

        }
    }
</script>

<style lang="less">
    /*修改分页样式*/

    .Cascader {
        width: 100%;
        display: block;
    }

    .ivu-poptip-rel{
        width:100%;
    }
</style>
